package com.example.luyang7415445.ly_15716test;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import com.baidu.location.LocationClientOption;
import com.example.luyang7415445.ly_15716test.MyScrollView.MyScrollViewListener;
import com.example.luyang7415445.ly_15716test.MyScrollView.OnBorderListener;
import com.example.luyang7415445.ly_15716test.OrderView.StayViewListener;
import com.nineoldandroids.animation.Animator;

public class MainActivity extends Activity implements MyScrollViewListener,OnBorderListener,OnClickListener{

    MyScrollView myScrollView;
    Button toTopBtn,btn_order;
    private Animator animator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toTopBtn = (Button) findViewById(R.id.top_btn);
        toTopBtn.setOnClickListener(this);
        btn_order = (Button) findViewById(R.id.btn_order);
        btn_order.setOnClickListener(this);
        OrderView refreshableView = (OrderView) findViewById(R.id.refreshview);
        myScrollView = (MyScrollView) findViewById(R.id.scrollview);
        myScrollView.setMyScrollViewListener(this);
        myScrollView.setOnBorderListener(this);
        refreshableView.setStayView(findViewById(R.id.theview),myScrollView,
                new StayViewListener() {
                    @Override
                    public void onStayViewShow() {
                        findViewById(R.id.theviewstay).setVisibility(
                                View.VISIBLE);

                    }

                    @Override
                    public void onStayViewGone() {
                        findViewById(R.id.theviewstay).setVisibility(View.GONE);

                    }
                });

    }

    @Override
    public void onScrollChanged(MyScrollView myScrollView, int x, int y,
                                int oldx, int oldy) {
        Log.e("onScrollChanged", "x:"+x+",y:"+y+",oldx:"+oldx+",oldy:"+oldy);


        if (y!=0 && y<oldy) {
            setAnimtoView(toTopBtn, 2);
        }else if (y!=0 && y>oldy) {
            setAnimtoView(toTopBtn, 1);
        }
    }

    @Override
    public void onBottom() {
        Log.e("onBottom","is bottom");
        setAnimtoView(toTopBtn, 1);
    }

    @Override
    public void onTop() {
        setAnimtoView(toTopBtn, 2);
        Log.e("onTop","is top");
    }

    private void setAnimtoView(Button btn,int type){

        if (type==1) {
            btn.setVisibility(View.VISIBLE);
            Animation animtion = AnimationUtils.loadAnimation(this, R.anim.down_to_up_anim);
            animtion.setFillAfter(true);
            btn.setAnimation(animtion);
        }else if(type == 2){
            btn.setVisibility(View.GONE);
            Animation animtion = AnimationUtils.loadAnimation(this, R.anim.up_to_down_anim);
            animtion.setFillAfter(true);
            btn.setAnimation(animtion);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.top_btn:
                myScrollView.scrollTo(0, 0);
                break;
            case R.id.btn_order:
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                break;

        }
    }
}
